import { request } from "../../request/index.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabInfos:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    // standard storage get of "bookmarks"
    const bookmarks = wx.getStorageSync("bookmarks");
    if (!bookmarks) {
      this.getBookmarks();
    } else {
      if (Date.now() - bookmarks.time > 1000 * 10) {
        this.getBookmarks();
      } else {
        this.setData({
          TabInfos: bookmarks.data
        })
      }
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getBookmarks();
  },

  getBookmarks() {
    // check login status
    const user_info = wx.getStorageSync('user_info')
    if (!user_info) {
      this.setData({
        TabInfos: []
      })
      wx.showModal({
        title: '您未登录',
        content: '是否跳转到登录界面？',
        success(res){
          if(res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        }
      })
      return
    }
    // send request
    const data = {
      mode: 2,
      user_id: user_info.id
    }
    // const res = await request({
    //   url: "/tabinfos",
    //   data: data
    // })
    const bookmarks = [
      {
        id: 0,
        goods_name: "Test Case",
        goods_price: 23.33,
        tags: [
          {
              id: 0,
              tag_name: "tag0",
              image_src: "/static/1.jpg"
          },
          {
              id: 2,
              tag_name: "tag2",
              image_src: "/static/2.jpeg"
          },
          {
              id: 85,
              tag_name: "tag85",
              image_src: "/static/3.jpeg"
          },
          {
              id: 166,
              tag_name: "tag166",
              image_src: "/static/c1.png"
          }
        ],
        image_src:"/static/g1.jpg",
        marked:false
      },
      {
        id: 2333,
        goods_name: "Test Case 2",
        goods_price: 2000,
        tags: [
          {
              id: 0,
              tag_name: "tag0",
              image_src: "/static/1.jpg"
          },
          {
              id: 2,
              tag_name: "tag2",
              image_src: "/static/2.jpeg"
          },
          {
              id: 85,
              tag_name: "tag85",
              image_src: "/static/3.jpeg"
          }
        ],
        image_src: "/static/g2.jpg",
        marked:true
      }
    ]
    this.setData({
      TabInfos: bookmarks
    })
    wx.setStorageSync('bookmarks', {time: Date.now, data: bookmarks})
  }

})